#include <stdio.h>
#include <stdlib.h>

struct ListNode {
	int data;
	struct ListNode *next;
};


ListNode *creat(int n) {
	struct ListNode *q, *head;
	int i = 1;
	struct ListNode *p = (struct ListNode *)malloc(sizeof(struct ListNode));
	p->data = i;
	head = p;
	for (i = 2; i <= n; i++) {
		q = (ListNode *)malloc(sizeof(struct ListNode));;
		q->data = i;
		p->next = q;
		p = q;
	}
	p->next = head;
	return head;
}

void print(ListNode *L, int m) {
	int i = 0;
	ListNode *p, *q, *s;
	p = L;
	while (p->next != p) {
		for (i = 1; i < m; i++) {
			q = p;
			p = p->next;
		}
		printf("%d\n", p->data);
		s = p;
		q->next = p->next;
		p = p->next;
		free(s);
	}
	printf("%5d", p->data);
}

int main() {
	ListNode *L;
	int n = 9, m = 5;
	L = creat(n);
	print(L, m);
	return 0;
}